<template>
	<xn-form-container
		:title="formData.id ? '编辑供应商' : '增加供应商'"
		:width="700"
		v-model:open="open"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-row :gutter="16">
				<a-col :span="12">
					<a-form-item label="供应商编码：" name="objectCode">
						<a-input v-model:value="formData.objectCode" placeholder="请输入供应商编码" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="供应商名称：" name="objectName">
						<a-input v-model:value="formData.objectName" placeholder="请输入供应商名称" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="对接负责人：" name="objectPerson">
						<xn-page-select
							ref="xnobjectPersonPageSelectRef"
							v-model:value="formData.objectPerson"
							placeholder="请选择负责人"
							allow-clear
							show-search
							:page-function="objectPersonSelector"
							:echo-function="objectPersonEcho"
						/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="联系人：" name="objectContact">
						<a-input v-model:value="formData.objectContact" placeholder="请输入联系人" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="联系电话：" name="objectTel">
						<a-input v-model:value="formData.objectTel" placeholder="请输入联系电话" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="邮箱：" name="objectEmail">
						<a-input v-model:value="formData.objectEmail" placeholder="请输入邮箱" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="开户行：" name="objectBank">
						<a-input v-model:value="formData.objectBank" placeholder="请输入开户行" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="银行账号：" name="objectBankCode">
						<a-input v-model:value="formData.objectBankCode" placeholder="请输入银行账号" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="地址：" name="supplierAddress">
						<a-input v-model:value="formData.objectAddress" placeholder="请输入地址" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="应付账款：" name="objectAccountsPayable">
						<a-input v-model:value="formData.objectAccountsPayable" placeholder="请输入应付账款" allow-clear :disabled="formData.id!==undefined" />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="备注：" name="objectRemark">
						<a-input v-model:value="formData.objectRemark" placeholder="请输入备注" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="状态：" name="objectStatus">
						<a-radio-group v-model:value="formData.objectStatus" :options="objectStatusOptions"/>
					</a-form-item>
				</a-col>
				<a-col :span="24">
					<a-form-item label="附件：" name="objectAttachment">
						<xn-upload v-model:value="formData.objectAttachment"  uploadMode="image" :uploadNumber="uploadNumber"/>
					</a-form-item>
				</a-col>
			</a-row>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="supplierSuppliersForm">
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import supplierObjectsApi from "@/api/supplier/custom/supplierObjectsApi";
	import {ref} from "vue";
	import supplierUserApi from "@/api/supplier/structure/supplierUserApi";

	// 抽屉状态
	const open = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)
	let uploadNumber = 10
	const objectStatusOptions = [{label:'停用', value:'0'},{label:'正常', value:'1'}]

	const xnobjectPersonPageSelectRef = ref();
	const objectPersonSelector = (param) => {
		return supplierUserApi.userPage(param).then((data) => {
			if (data.records) {
				data.records = data.records.map((item) => {
					return {
						name: item.name,
						id: item.id,

					};
				});
			}
			return Promise.resolve(data)
		})
	}
	const objectPersonEcho = (param) => {
		let echoList = []
		let echo =  {}
		for(var i=0;i<param.idList[0].length;i++){
			echo.name = formData.value.objectPersonName
			echo.id = param.idList[0][i]
			echoList.push(echo)
		}
		return Promise.resolve(echoList)
	}
	// 打开抽屉
	const onOpen = (record) => {

		open.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
		nextTick(() => {
			xnobjectPersonPageSelectRef.value.onPage({current: 1});
		})
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		open.value = false
	}
	// 默认要校验的
	const formRules = {
		objectCode: [required('请输入供应商编码')],
		objectName: [required('请输入供应商名称')],
		objectContact: [required('请输入联系人')],
		objectTel: [required('请输入联系电话')],
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				const formDataParam = cloneDeep(formData.value)
				formDataParam.objectType = "0" //供应商
				supplierObjectsApi
					.supplierObjectsSubmitForm(formDataParam, formDataParam.id)
					.then(() => {
						onClose()
						emit('successful')
					})
					.finally(() => {
						submitLoading.value = false
					})
			})
			.catch(() => {})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
